package com.zhc.dejingapi.utils.qing;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.zhc.dejingapi.entity.BommakeEntity;
import com.zhc.dejingapi.entity.QingAppfieldsEntity;
import com.zhc.dejingapi.mapper.BommakeMapper;
import com.zhc.dejingapi.mapper.QingAppfieldsMapper;
import com.zhc.dejingapi.mapper.QingAppfieldtablevaluesMapper;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.pojo.qingflow.appdata.adddata.*;
import com.zhc.dejingapi.pojo.qingflow.appdata.deldata.QingDelDataModel;
import com.zhc.dejingapi.pojo.qingflow.appdata.editdata.QingEditDataFieldModel;
import com.zhc.dejingapi.pojo.qingflow.appdata.editdata.QingEditDataFieldValueModel;
import com.zhc.dejingapi.pojo.qingflow.appdata.editdata.QingEditDataModel;
import org.springframework.jdbc.core.JdbcTemplate;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class BomMakeUploadUtils {
    public BomMakeUploadUtils(QingflowApiInfo qqingflowApiInfo1, JdbcTemplate mysqlJdbcTemplate1,
                              QingAppfieldsMapper qingAppfieldsMapper1,
                              QingAppfieldtablevaluesMapper qingAppfieldTableValuesMapper1,
                              BommakeMapper bommakeMapper1)throws Exception{
        this.qqingflowApiInfo=qqingflowApiInfo1;
        this.mysqlJdbcTemplate=mysqlJdbcTemplate1;
        this.qingAppfieldsMapper=qingAppfieldsMapper1;
        this.qingAppfieldtablevaluesMapper = qingAppfieldTableValuesMapper1;
        this.bommakeMapper=bommakeMapper1;
        LoadInfo();
    }
    public BomMakeUploadUtils(QingflowApiInfo qqingflowApiInfo1, JdbcTemplate mysqlJdbcTemplate1,
                              QingAppfieldsMapper qingAppfieldsMapper1,
                              QingAppfieldtablevaluesMapper qingAppfieldTableValuesMapper1,
                              BommakeMapper bommakeMapper1,
                              String  strErpTableName)throws Exception{

        this.qqingflowApiInfo=qqingflowApiInfo1;
        this.mysqlJdbcTemplate=mysqlJdbcTemplate1;
        this.qingAppfieldsMapper=qingAppfieldsMapper1;
        this.qingAppfieldtablevaluesMapper = qingAppfieldTableValuesMapper1;
        this.bommakeMapper=bommakeMapper1;
        if (!(strErpTableName==null||strErpTableName.isEmpty())){
            this.erpTableName=strErpTableName;
        }
        LoadInfo();
    }



    private String erpTableName="bommake";
    private Long appMainId;
    private String appKey;
    private JdbcTemplate mysqlJdbcTemplate;
    private QingflowApiInfo qqingflowApiInfo;

    private QingAppfieldsMapper qingAppfieldsMapper;

    private QingAppfieldtablevaluesMapper qingAppfieldtablevaluesMapper;
    private BommakeMapper bommakeMapper;

    private List<QingAppfieldsEntity> listFields=null;
    private void LoadInfo() throws Exception{


        String strSql="SELECT appMainId\n" +
                "from  appinferpinfocontrast\n" +
                "where erpTableName=?";
        appMainId=mysqlJdbcTemplate.queryForObject(strSql,Long.class,erpTableName);
        if (appMainId==null){
            throw new Exception("ERP表名"+erpTableName+"没有找到对应的appMainId");
        }
        strSql="SELECT appKey\n" +
                "from qing_appinfo\n" +
                "where main_id=?";
        appKey=mysqlJdbcTemplate.queryForObject(strSql,String.class,appMainId);
        if (appKey==null||appKey.isEmpty()){
            throw new Exception("ERP表名"+erpTableName+"没有找到对应的appKey");
        }

        QueryWrapper<QingAppfieldsEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("appMain_Id",appMainId)
                .gt("queId",1000000);


        listFields=qingAppfieldsMapper.selectList(queryWrapper);
        if (listFields==null||listFields.isEmpty()){
            throw new Exception("ERP表名"+erpTableName+"没有找到对应的字段信息");
        }


    }

    public boolean AddBomMake(BommakeEntity bomMakeEntity){
        boolean isSuccess=false;
        try{
            //String strAccessToken=accessTokenUtils.getAccessToken(qqingflowApiInfo);

            QingAddDataModel qingAddDataModel =new QingAddDataModel();

            QingAddDataUserModel dataUserModel=new QingAddDataUserModel();
            dataUserModel.setMobile(qqingflowApiInfo.getUserMobile());
            dataUserModel.setEmail(qqingflowApiInfo.getUserEmail());
            dataUserModel.setAreaCode(qqingflowApiInfo.getUserAreacode());
            qingAddDataModel.setApplyUser(dataUserModel);


            List<QingAddDataFieldModel> listFieldModels=new ArrayList<>();
            for (int i = 0; i < listFields.size(); i++) {
                QingAppfieldsEntity sourceEntity=listFields.get(i);
                QingAddDataFieldModel addDataFieldModel=new QingAddDataFieldModel();
                QingAddDataFieldValueModel addDataFieldValueModel=new QingAddDataFieldValueModel();

                if (sourceEntity.getQueTitle().equals("单位编码")){

                    addDataFieldModel=CopyAddData(sourceEntity,bomMakeEntity.getCode());
                    listFieldModels.add(addDataFieldModel);
                }
                else  if (sourceEntity.getQueTitle().equals("单位名称")){
                    addDataFieldModel=CopyAddData(sourceEntity,bomMakeEntity.getName());
                    listFieldModels.add(addDataFieldModel);
                }
                else  if (sourceEntity.getQueTitle().equals("说明")){
                    addDataFieldModel=CopyAddData(sourceEntity,bomMakeEntity.getDescrip());
                    listFieldModels.add(addDataFieldModel);
                }
                else  if (sourceEntity.getQueTitle().equals("状态")){
                    if (bomMakeEntity.getUpdAction().toUpperCase().equals("D")){
                        addDataFieldModel=CopyAddData(sourceEntity,"已删除");
                    }
                    else{
                        addDataFieldModel=CopyAddData(sourceEntity,"可用");
                    }
                    listFieldModels.add(addDataFieldModel);
                }
                else  if (sourceEntity.getQueTitle().equals("标识码")){
                    addDataFieldModel=CopyAddData(sourceEntity,bomMakeEntity.getEri());
                    listFieldModels.add(addDataFieldModel);
                }

                //listFieldModels.add(addDataFieldModel);
            }
            qingAddDataModel.setAnswers(listFieldModels);

            //转字符串
            Gson gson = new Gson();

            //String strJson= JSON.toJSONString(qingAddDataModel);
            String strJson=gson.toJson(qingAddDataModel);


                    //获取返回结果
            QingAddDataResultMain resultMain= QingflowAPIUtils.AddDataToQing(qqingflowApiInfo,appKey,strJson);

            if (resultMain==null){

                bomMakeEntity.setErrCode("-10015");
                bomMakeEntity.setErrMsg("服务器返回内容为空！");
                bomMakeEntity.setLastRequestId(null);
                bomMakeEntity.setIsDeal(0);
                bomMakeEntity.setIsWaiting(0);
                bomMakeEntity.setDealDate(LocalDateTime.now());
                bommakeMapper.updateById(bomMakeEntity);

            }
            else{
                if (resultMain.getErrCode()==0){
                    bomMakeEntity.setErrCode(resultMain.getErrCode().toString());
                    bomMakeEntity.setErrMsg("提交成功！");
                    bomMakeEntity.setLastRequestId(resultMain.getResult().getRequestId());
                    bomMakeEntity.setIsDeal(0);
                    bomMakeEntity.setIsWaiting(1);
                    bomMakeEntity.setDealDate(LocalDateTime.now());
                    bomMakeEntity.setActionResult("A");
                    bommakeMapper.updateById(bomMakeEntity);

                }
                else{
                    bomMakeEntity.setErrCode(resultMain.getErrCode().toString());
                    bomMakeEntity.setErrMsg(resultMain.getErrMsg());
                    bomMakeEntity.setLastRequestId(null);
                    bomMakeEntity.setIsDeal(0);
                    bomMakeEntity.setIsWaiting(0);
                    bomMakeEntity.setDealDate(LocalDateTime.now());
                    bommakeMapper.updateById(bomMakeEntity);
                }

            }

        }
        catch (Exception e){
            e.printStackTrace();
            //throw  e;
        }
        return isSuccess;
    }

    public boolean UpdBomMake(BommakeEntity bomMakeEntity,String strActionResult){
        boolean isSuccess=false;
        try{
            //String strAccessToken=accessTokenUtils.getAccessToken(qqingflowApiInfo);

            QingEditDataModel qingEditDataModel =new QingEditDataModel();

            QingAddDataUserModel dataUserModel=new QingAddDataUserModel();



            List<QingEditDataFieldModel> listFieldModels=new ArrayList<>();
            for (int i = 0; i < listFields.size(); i++) {
                QingAppfieldsEntity sourceEntity=listFields.get(i);
                QingEditDataFieldModel addDataFieldModel=new QingEditDataFieldModel();

                if (sourceEntity.getQueTitle().equals("单位编码")){
                    QingEditDataFieldValueModel addDataFieldValueModel=new QingEditDataFieldValueModel();

                    addDataFieldValueModel.setValue(bomMakeEntity.getCode());
                    List<QingEditDataFieldValueModel> listValues=new ArrayList<>();


                    listValues.add(addDataFieldValueModel);
                    addDataFieldModel.setQueId(sourceEntity.getQueId());
                    addDataFieldModel.setValues(listValues);
                }
                else  if (sourceEntity.getQueTitle().equals("单位名称")){
                    QingEditDataFieldValueModel addDataFieldValueModel=new QingEditDataFieldValueModel();

                    addDataFieldValueModel.setValue(bomMakeEntity.getName());
                    List<QingEditDataFieldValueModel> listValues=new ArrayList<>();

                    listValues.add(addDataFieldValueModel);
                    addDataFieldModel.setQueId(sourceEntity.getQueId());
                    addDataFieldModel.setValues(listValues);
                }
                else  if (sourceEntity.getQueTitle().equals("说明")){
                    QingEditDataFieldValueModel addDataFieldValueModel=new QingEditDataFieldValueModel();

                    addDataFieldValueModel.setValue(bomMakeEntity.getDescrip());
                    List<QingEditDataFieldValueModel> listValues=new ArrayList<>();

                    listValues.add(addDataFieldValueModel);
                    addDataFieldModel.setQueId(sourceEntity.getQueId());
                    addDataFieldModel.setValues(listValues);
                }
                else  if (sourceEntity.getQueTitle().equals("状态")){
                    QingEditDataFieldValueModel addDataFieldValueModel=new QingEditDataFieldValueModel();

                    addDataFieldValueModel.setValue(bomMakeEntity.getCode());

                    if (bomMakeEntity.getUpdAction().toUpperCase().equals("D")){
                        //addDataFieldModel=CopyAddData(sourceEntity,"已删除");
                        addDataFieldValueModel.setValue("已删除");
                    }
                    else{
                        //addDataFieldModel=CopyAddData(sourceEntity,"可用");
                        addDataFieldValueModel.setValue("可用");
                    }
                    List<QingEditDataFieldValueModel> listValues=new ArrayList<>();

                    listValues.add(addDataFieldValueModel);
                    addDataFieldModel.setQueId(sourceEntity.getQueId());
                    addDataFieldModel.setValues(listValues);
                }
                //else  if (sourceEntity.getQueTitle().equals("标识码")){
                //    addDataFieldModel=CopyAddData(sourceEntity,bomMakeEntity.getEri());
                // listFieldModels.add(addDataFieldModel);
                //}

                listFieldModels.add(addDataFieldModel);
            }

            qingEditDataModel.setAnswers(listFieldModels);


            //转字符串
            //String strJson= JSON.toJSONString(qingEditDataModel);
            Gson gson = new Gson();
            String strJson=gson.toJson(qingEditDataModel);
            //获取返回结果
            QingAddDataResultMain resultMain= QingflowAPIUtils.UpdDataToQing(qqingflowApiInfo,appKey,strJson,bomMakeEntity.getApplyId()+"");

            if (resultMain==null){

                bomMakeEntity.setErrCode("-10015");
                bomMakeEntity.setErrMsg("服务器返回内容为空！");
                bomMakeEntity.setLastRequestId(null);
                bomMakeEntity.setIsDeal(0);
                bomMakeEntity.setIsWaiting(0);
                bomMakeEntity.setDealDate(LocalDateTime.now());
                bommakeMapper.updateById(bomMakeEntity);

            }
            else{
                if (resultMain.getErrCode()==0){
                    bomMakeEntity.setErrCode(resultMain.getErrCode().toString());
                    bomMakeEntity.setErrMsg("提交成功！");
                    bomMakeEntity.setLastRequestId(resultMain.getResult().getRequestId());
                    bomMakeEntity.setIsDeal(0);
                    bomMakeEntity.setIsWaiting(1);
                    bomMakeEntity.setDealDate(LocalDateTime.now());
                    bomMakeEntity.setActionResult(strActionResult);
                    bommakeMapper.updateById(bomMakeEntity);
                    //增加马上判断的结果

                }
                else{
                    bomMakeEntity.setErrCode(resultMain.getErrCode().toString());
                    bomMakeEntity.setErrMsg(resultMain.getErrMsg());
                    bomMakeEntity.setLastRequestId(null);
                    bomMakeEntity.setIsDeal(0);
                    bomMakeEntity.setIsWaiting(0);
                    bomMakeEntity.setDealDate(LocalDateTime.now());
                    bommakeMapper.updateById(bomMakeEntity);
                }

            }

        }
        catch (Exception e){
            e.printStackTrace();
            //throw  e;
        }
        return isSuccess;

    }

    public boolean DelBomMake(BommakeEntity bomMakeEntity){
        boolean isSuccess=false;
        try{
            //String strAccessToken=accessTokenUtils.getAccessToken(qqingflowApiInfo);

            QingDelDataModel qingAddDataModel =new QingDelDataModel();

            List<Long> listDel=new ArrayList<>();
            listDel.add(bomMakeEntity.getApplyId());

            qingAddDataModel.setApplyIds(listDel);
            //转字符串
            //String strJson= JSON.toJSONString(qingAddDataModel);
            Gson gson = new Gson();
            String strJson=gson.toJson(qingAddDataModel);
            //获取返回结果
            //qingflowApiInfo qingflowApiInfo, String appKey,String strBody
            QingAddDataResultMain resultMain= QingflowAPIUtils.DelDataToQing(qqingflowApiInfo,appKey,strJson);

            if (resultMain==null){

                bomMakeEntity.setErrCode("-10015");
                bomMakeEntity.setErrMsg("服务器返回内容为空！");
                bomMakeEntity.setLastRequestId(null);
                bomMakeEntity.setIsDeal(0);
                bomMakeEntity.setIsWaiting(0);
                bomMakeEntity.setDealDate(LocalDateTime.now());
                bommakeMapper.updateById(bomMakeEntity);

            }
            else{
                if (resultMain.getErrCode()==0){
                    bomMakeEntity.setErrCode(resultMain.getErrCode().toString());
                    bomMakeEntity.setErrMsg("提交成功！");
                    bomMakeEntity.setLastRequestId(resultMain.getResult().getRequestId());
                    bomMakeEntity.setIsDeal(0);
                    bomMakeEntity.setIsWaiting(1);
                    bomMakeEntity.setDealDate(LocalDateTime.now());
                    bomMakeEntity.setActionResult("D");
                    bommakeMapper.updateById(bomMakeEntity);
                    //增加马上判断的结果

                }
                else{
                    bomMakeEntity.setErrCode(resultMain.getErrCode().toString());
                    bomMakeEntity.setErrMsg(resultMain.getErrMsg());
                    bomMakeEntity.setLastRequestId(null);
                    bomMakeEntity.setIsDeal(0);
                    bomMakeEntity.setIsWaiting(0);
                    bomMakeEntity.setDealDate(LocalDateTime.now());
                    bommakeMapper.updateById(bomMakeEntity);
                }

            }

        }
        catch (Exception e){
            e.printStackTrace();
            //throw  e;
        }
        return isSuccess;
    }


    public QingAddDataFieldModel CopyAddData(QingAppfieldsEntity fieldEntity,String fieldSetValue){
        QingAddDataFieldModel addDataFieldModel=new QingAddDataFieldModel();

        QingAddDataFieldValueModel addDataFieldValueModel=new QingAddDataFieldValueModel();
        //"dataValue": "可用",
        addDataFieldValueModel.setDataValue(fieldSetValue);
        //"email": null,
        addDataFieldValueModel.setEmail(fieldEntity.getValueEmail());
        //"id": null,
        addDataFieldValueModel.setId(fieldEntity.getValueId());
        //"optionId": null,
        addDataFieldValueModel.setOptionId(fieldEntity.getValueOptionid());
        //"ordinal": null,
        addDataFieldValueModel.setOrdinal(fieldEntity.getValueOrdinal());
        //"otherInfo": null,
        addDataFieldValueModel.setOtherInfo(fieldEntity.getValueOtherinfo());
        //"pluginValue": null,
        addDataFieldValueModel.setPluginValue(fieldEntity.getValuePluginvalue());
        //"queId": 208482472,
        addDataFieldValueModel.setQueId(fieldEntity.getValueQueid());
        //"value": "可用"
        addDataFieldValueModel.setValue(fieldSetValue);


        List<QingAddDataFieldValueModel> list=new ArrayList<>();
        list.add(addDataFieldValueModel);

        //"queId": 208482493,
        addDataFieldModel.setQueId(fieldEntity.getQueId());
        //"queTitle": "分类编号",
        addDataFieldModel.setQueTitle(fieldEntity.getQueTitle());
        //"queType": "2",
        addDataFieldModel.setQueType(fieldEntity.getQueType());
        //"values":
        addDataFieldModel.setValues(list);

        return addDataFieldModel;
    }
}
